package org.i_plus;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.i_plus.model.HobbyBean;

import sun.misc.BASE64Encoder;

public class Assistance {
	String ball,run,read,swim,movie,sing,bike,tvgame;
	private String bloodType;
	public static String photoByteToImgString(String imgType, byte[] infile) {
			if (infile != null && imgType != null && infile.length != 0
					&& imgType.length() != 0) {
				BASE64Encoder encoder = new BASE64Encoder(); // 实例化一个对byte[]型数据进行64位编码的对象
				String blob = encoder.encode(infile); // 对bytes进行编码，bytes即为数据库中获取的图片数据;
				String resutlt = imgType + ";base64," + blob;// 檔案格式為
																// 
				return resutlt;
			}
			return "";
		}
	public int howOldAreYou(java.sql.Date date)
	{
		long now = System.currentTimeMillis();
		long userlong = date.getTime();
		long result= now - userlong;
		long yearsold =((result)/(new Long("31536000000")));
		int big=(int)yearsold;
		return big;
	}
	public String howSignAreYou(java.sql.Date date)
	{	
		String starder=null;
		String trans=date.toString();
		String month=trans.substring(5,7);
		int day=date.getDate(); 
		if(month.equals("01"))
		{
			if(day>=20)
			{
				starder="水瓶座";
			}else{
				starder="魔羯座";
			}
		}
		if(month.equals("02"))
		{
			if(day>=19)
			{
				starder="雙魚座";
			}else{
				starder="水瓶座";
			}
		}
		if(month.equals("03"))
		{
			if(day>=21)
			{
				starder="牡羊座";
			}else{
				starder="雙魚座";
			}
		}
		if(month.equals("04"))
		{
			if(day>=20)
			{
				starder="金牛座";
			}else{
				starder="牡羊座";
			}
		}
		if(month.equals("05"))
		{
			if(day>=21)
			{
				starder="雙子座";
			}else{
				starder="金牛座";
			}
		}
		if(month.equals("06"))
		{
			if(day>=22)
			{
				starder="巨蟹座";
			}else{
				starder="雙子座";
			}
		}
		if(month.equals("07"))
		{
			if(day>=23)
			{
				starder="獅子座";
			}else{
				starder="巨蟹座";
			}
		}
		if(month.equals("08"))
		{
			if(day>=23)
			{
				starder="處女座";
			}else{
				starder="獅子座";
			}
		}
		if(month.equals("09"))
		{
			if(day>=23)
			{
				starder="天秤座";
			}else{
				starder="處女座";
			}
		}
		if(month.equals("10"))
		{
			if(day>=24)
			{
				starder="天蠍座";
			}else{
				starder="天秤座";
			}
		}
		if(month.equals("11"))
		{
			if(day>=23)
			{
				starder="射手座";
			}else{
				starder="天蠍座";
			}
		}
		if(month.equals("22"))
		{
			if(day>=23)
			{
				starder="魔羯座";
			}else{
				starder="射手座";
			}
		}
		return starder;
	}
	public String hobbyTrans(HobbyBean bean)
	{
		if(bean.isBall()==true)
		{
			String ball="球類";
			this.ball=ball;
		}else{
			String ball="";
			this.ball=ball;
		}
		if(bean.isRun()==true)
		{
			String run="跑步";
			this.run=run;
		}else{
			String run="";
			this.run=run;
		}
		if(bean.isReading()==true)
		{
			String read="閱讀";
			this.read=read;
		}else{
			String read="";
			this.read=read;
		}
		if(bean.isSwim()==true)
		{
			String swim="游泳";
			this.swim=swim;
		}else{
			String swim="";
			this.swim=swim;
		}
		if(bean.isMovie()==true)
		{
			String movie="電影";
			this.movie=movie;
		}else{
			String movie="";
			this.movie=movie;
		}
		if(bean.isSinging()==true)
		{
			String sing="唱歌";
			this.sing=sing;
		}else{
			String sing="";
			this.sing=sing;
		}
		if(bean.isBike()==true)
		{
			String bike="單車";
			this.bike=bike;
		}else{
			String bike="";
			this.bike=bike;
		}		
		if(bean.isTvgame()==true)
		{
			String tvgame="電玩";
			this.tvgame=tvgame;
		}else{
			String tvgame="";
			this.tvgame=tvgame;
		}
		String total=ball+" "+run+" "+read+" "+swim+" "+movie+" "+sing+" "+bike+" "+tvgame;
		return total;
	}
	public  String howTypeBlood(String blood)
	{	
		if(blood.equals("C"))
		{
			 this.bloodType="AB";
		}else{
			this.bloodType=blood;
		}
		return bloodType;
	}
	public static byte[] cropper(byte[] pic ,int top, int left,  int width, int height){
			byte[] result = null;
			 ByteArrayOutputStream baos = null;
			try {
				InputStream in = new ByteArrayInputStream(pic);		
				BufferedImage bi =ImageIO.read(in);//將得到的圖片轉成BufferedImage
				
				  height = Math.min(height, bi.getHeight());     
				  width = Math.min(width, bi.getWidth());     
				  if(height <= 0) height = bi.getHeight();     
				  if(width <= 0) width = bi.getWidth();     
				  top = Math.min(Math.max(0, top), bi.getHeight()-height);     
				  left = Math.min(Math.max(0, left), bi.getWidth()-width);     
				       
				  BufferedImage bi_cropper = bi.getSubimage(left, top, width, height);   //已切完  
				  baos = new ByteArrayOutputStream();
				  ImageIO.write( bi_cropper, "jpg", baos );//將BufferedImage轉回byte
				  result = baos.toByteArray();
				  baos.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				if (baos!=null) {
					try {
						baos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			  return result;   
	}
	
	public static void main(String[] args) {
		Assistance ass=new Assistance();
		String s=ass.howSignAreYou(java.sql.Date.valueOf("1984-01-01"));
		System.out.println(s);
	}
}
